import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import stub.Product;
import stub.Warehouse;


public class Client{
	
	private String		customerName;
	private Long		sessionId;
	private Warehouse	stub;
	private Scanner		scanner;
	private String		selectedProductType;
	
	
	public Client( Warehouse stub ) {
		this.stub = stub;
		
		// Initialize a scanner to read input
		scanner = new Scanner( System.in );
		System.out.println( "Welkom in de winkel, wat is uw naam?" );
		customerName = scanner.next();
		try{
			sessionId = stub.newOrder( customerName );
			
			startWarehouse();
			
		} catch( RemoteException e ){
			e.printStackTrace();
		}
	}
	
	
	public void startWarehouse() throws RemoteException {
		System.out.println( "[1] Toon alle producten" );
		System.out.println( "[2] Zoek producten op type" );
		System.out.println( "[3] Toon bestel overzicht" );
		System.out.println( "[0] exit" );
		int selection = readInput( 3 );
		switch( selection ){
		
			case 1:
				showProducts();
				break;
			
			case 2:
				showProductsWithType();
				break;
			
			case 3:
				showOrderOverview();
				break;
			
			case 0:
				new Client( stub );
				break;
			
			default:
				
		}
	}
	
	
	public void showProducts() throws RemoteException {
		List<Product> result = stub.getProducten();
		
		System.out.println( "Selecteer een product om deze te bestellen" );
		for( int i = 0; i < result.size(); i++ ){
			System.out.println( String
					.format( "%-30s %-4s", "[" + ( i + 1 ) + "] " + result.get( i ).getName(), result.get( i ).getPrice() ) );
		}
		System.out.println( "[0] terug" );
		
		int selection = readInput( result.size() );
		if( selection == 0 ){
			startWarehouse();
		} else{
			orderProduct( result.get( selection - 1 ), "allProducts" );
		}
	}
	
	
	public void showProductsWithType() throws RemoteException {
		List<String> receivedTypes = stub.getProductTypes();
		System.out.println( "Selecteer een nummer om producten van dat type te zien" );
		for( int i = 0; i < receivedTypes.size(); i++ ){
			System.out.println( "[" + ( i + 1 ) + "] " + receivedTypes.get( i ) );
		}
		System.out.println( "[0] terug" );
		
		int selection = readInput( receivedTypes.size() );
		if( selection == 0 ){
			startWarehouse();
		} else{
			selectedProductType = receivedTypes.get( selection - 1 );
			showSpecificProducts();
			
		}
	}
	
	
	public void showSpecificProducts() throws RemoteException {
		List<Product> result = stub.getProducten( selectedProductType );
		System.out.println( "Selecteer een product om deze te bestellen" );
		for( int i = 0; i < result.size(); i++ ){
			System.out.println( "[" + ( i + 1 ) + "] " + result.get( i ).getName() + "\t" + result.get( i ).getPrice() );
		}
		System.out.println( "[0] terug" );
		
		int selection = readInput( result.size() );
		if( selection == 0 ){
			showProductsWithType();
		} else{
			orderProduct( result.get( selection - 1 ), "productTypes" );
		}
	}
	
	
	public void orderProduct( Product selectedProduct, String callbackType ) throws RemoteException {
		System.out.println( "Hoeveel wilt u er? (druk 0 om terug te gaan)" );
		System.out.println( "Naam:" + selectedProduct.getName() + "\t prijs:" + selectedProduct.getPrice() );
		
		int selection = readInput( 99 );
		if( selection == 0 ){
			showProducts();
		} else{
			stub.order( customerName, sessionId, selectedProduct.getName(), selection );
			if( callbackType.equals( "productTypes" ) ){
				showSpecificProducts();
			} else{
				showProducts();
			}
		}
	}
	
	
	public void showOrderOverview() throws RemoteException {
		Map<Product, Integer> orderedProducts = stub.getOrderOverview( customerName, sessionId );
		System.out.println( String.format( "%-25s %-5s %-5s", "naam", "prijs", "aantal" ) );
		for( Product p : orderedProducts.keySet() ){
			System.out.println( String.format( "%-25s %-5s %-5s", p.getName(), p.getPrice(), +orderedProducts.get( p ) ) );
		}
		
		System.out.printf( "Totaal: %.2f", stub.getTotalPriceForOrder( customerName, sessionId ) );
		System.out.println( "\n[1] bevestig bestelling" );
		System.out.println( "[0] terug" );
		
		int selection = readInput( 1 );
		if( selection == 0 ){
			startWarehouse();
		} else{
			if( stub.secureOrder( customerName, sessionId ) ){
				System.out.println( "Bestelling voltooid" );
				sessionId = stub.newOrder( customerName );
				startWarehouse();
			}
		}
	}
	
	
	public int readInput( int max ) {
		int selection = 0;
		while( true ){
			try{
				selection = Integer.parseInt( scanner.next() );
				if( selection > max || selection < 0 ){
					throw new NumberFormatException();
				}
				return selection;
				
			} catch( NumberFormatException e ){
				System.out.println( "incorrecte input probeer opnieuw" );
			}
		}
	}
}